package swu.edu.cn.javaee.controller;


import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import swu.edu.cn.javaee.entity.User;
import swu.edu.cn.javaee.entity.UserInfo;
import swu.edu.cn.javaee.entity.UserLogin;
import swu.edu.cn.javaee.service.UserService;

import static swu.edu.cn.javaee.component.Signal.*;


@Controller
//解决跨域问题：允许所有源域名访问
@CrossOrigin(origins = "*")
public class UserController {


    @Resource
    private UserService userService;

    /**
     * 注册请求
     * params String String String
     * return String
     * */
    @RequestMapping("/register")
    @ResponseBody
    public String register(String phone,String password,String permission){
        String result=userService.saveUser(phone,password,permission);
        return result;
    }


    /**
     * 登录请求
     * params String String
     * return String
     * */
    @RequestMapping("/login")
    @ResponseBody
    public UserLogin login(String phone, String password){
        UserLogin result=userService.checkUser(phone,password);
        return result;
    }



    /**
     * 修改个人信息请求
     * params String String String
     * return String
     * */
    @RequestMapping("/changeInformation")
    @ResponseBody
    public String changeInformation(String phone,String avatar,String nickname){
        String result=userService.changeUser(phone,avatar,nickname);
        return result;
    }
    /**
     * 密码管理请求
     * params String String
     * return String
     * */
    @RequestMapping("/changePassword")
    @ResponseBody
    public String changePassword(String phone,String password){
        String result=userService.changePassword(phone,password);
        return result;
    }

    /**
     * 展示个人信息请求
     * params String
     * return User
     * */
    @RequestMapping("/userInformation")
    @ResponseBody
    public User userInformation(String phone){
        User user=new User();
        user=userService.queryUser(phone);
        if(user!=null) {
            /*System.out.println(user);*/
            return user;
        }
        else{
            return null;
        }
    }


    /**
     *展示所有用户个人信息
     * return userInfo/null
     * */
    @RequestMapping("/userAllInformation")
    @ResponseBody
    public UserInfo userAllInformation(){
        UserInfo userInfo=userService.queryList();
        if(userInfo!=null){
            System.out.println(userInfo);
            return userInfo;
        }
        else {
            return null;
        }
    }

    //分页查询请求
    @RequestMapping("/userLimitInformation")
    @ResponseBody
    public UserInfo userLimitInformation(@RequestParam(value = "page",required = true) String page,
                                         @RequestParam(value = "limit",required = true) String limit){
        UserInfo userInfo=userService.queryListLimit(page,limit);
        return userInfo;
    }
}


/**
 * 查询所有用户
 * 192.168.40.68:7071/userAllInformation
 * return UserInfo （code，msg，count，data）或null
 * */

/**
 * 登录
 * 192.168.40.68:7071/login
 * params   phone:xxx  password:xxx
 * return   String(返回一个字符串)
 * */

/**
 * 注册
 * 192.168.40.68:7071/register
 * params  phone:xxx  password:xxx permission:xxx
 * return  String
 * */